package com.hnu.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.hnu.model.User;
import com.hnu.model.UserDAO;
import com.hnu.service.UserServ;

public class UserServImpl implements UserServ {
	private UserDAO userDAO;

	public boolean addUser(User user) {
		int count = this.userDAO.findByProperty("userName", user.getUserName()).size();
		if(count > 0)
		{
			return false;
		}
		else
		{
			this.userDAO.save(user);
			return true;
		}
	}

	public boolean updateUser(User user) {
		this.userDAO.merge(user);
		return true;
	}

	public User verifyLogin(String userName) {
		List<User> userList = new ArrayList<User>();
		userList.addAll(this.userDAO.findByProperty("userName", userName));
		if(!userList.isEmpty())
		{
			return userList.get(0);
		}
		else
		{
			return null;
		}
	}
	
	public User getUserByUserId(long userId) {
		User user = this.userDAO.findById(userId);
		return user;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

}
